package day04;

import day04.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import java.sql.Connection;

public class Demo03DruildUtils {
    @Test
    public void Insert() throws Exception {
        QueryRunner queryRunner = new QueryRunner();
        //获取连接
        Connection connection = DruidUtils.getConnection();
        String sql = "insert into category(cname) values (?)";
        queryRunner.update(connection, sql, "西瓜");
        DruidUtils.close(connection, null, null);
    }

    @Test
    public void delete() throws Exception {
        QueryRunner qr = new QueryRunner();
        //1.获取连接对象
        Connection conn = DruidUtils.getConnection();
        //2.准备sql
        String sql = "delete from category where cid = ?";
        //3.执行sql
        qr.update(conn, sql, 203);
        //4.关闭资源
        DruidUtils.close(conn, null, null);

    }
    @Test
    public void update() throws Exception {
        QueryRunner qr = new QueryRunner();
        //1.获取连接对象
        Connection conn = DruidUtils.getConnection();
        //2.准备sql
        String sql = "update category set cname = ? where cid = ?";
        //3.执行sql
        qr.update(conn, sql, "手机", 204);
        //4.关闭资源
        DruidUtils.close(conn, null, null);
    }
    @Test
    public void Insert1() throws  Exception{
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        String sql = "insert into category(cname) values (?)";
        queryRunner.update(sql, "橘子");

    }

}
